<?php
namespace app\send\logic\msg;
use app\send\logic\msg\Msg;
use \think\Config;
/**
 * 短信宝
 */
class SmsbaoMsg extends Msg
{
    /**
     * [发送验证码]
     * @param  [type] $phone [手机号]
     * @param  [type] $code  [验证码]
     * @return [type]        [description]
     */
	public function index($phone,$code){
        header("Content-Type:text/html;charset=utf-8");        
        $statusStr = array(  
            "0" => "短信发送成功",  
            "-1" => "参数不全",  
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",  
            "30" => "账号或密码错误",  
            "40" => "账号不存在",  
            "41" => "余额不足",  
            "42" => "帐户已过期",  
            "43" => "IP地址限制",  
            "50" => "内容含有敏感词"  
        );  
        $config = Config::get('smsbaocode');
        $smsapi = "http://api.smsbao.com/"; //短信网关  
        $user = $config['user']; //短信平台帐号  
        $pass = md5($config['pass']); //短信平台密码  
        $content = "【".$config['sign']."】您的验证码是".$code.",180秒内有效.若非本人操作请忽略此消息。";
        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);  
        $result =$this->getHttp($sendurl) ; 
        //短信发送后，可以将发送内容等记录到数据库中  
        // echo $statusStr[$result];  
        if($result=='0')  
        {  
           return [
                'code'=>1,
            ];
        }else{  
           return [
                'code'=>0,
                'msg'=>$statusStr[$result]
           ];
        }  
    }
    public function getHttp($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
     }
    
}

